﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;
using CIT.WMS.Transefer;

namespace CIT.WMS.Product
{
    public partial class FrmProductIn : Form
    {
        private string Area_SN;
        private string Storage_SN;

        private string realPartNumber;
        string realLocationSN;
        string realDateCode;

        public FrmProductIn()
        {
            InitializeComponent();
        }
        DataTable DtIn;
        private void FrmProductIn_Load(object sender, EventArgs e)
        {
            BindCBX();
            cbx_Doc.Focus();
        }

        public void BindCBX()
        {
            DtIn = Bll.Bll_ProductIn.GetInStockProduct();

            cbx_Doc.Text = "";
            cbx_Doc.Items.Clear();

            if (DtIn.Rows.Count > 0)
            {
                foreach (DataRow dr in DtIn.Rows)
                {
                    cbx_Doc.Items.Add(dr["BillNo"].ToString());
                }
            }

        }

        private void cbx_Doc_SelectedIndexChanged(object sender, EventArgs e)
        {

            InputBillNo();
        }

        public void InputBillNo()
        {
            DataRow[] drIn = DtIn.Select(string.Format(" BillNo = '{0}'", cbx_Doc.Text.ToString()));
            if (drIn.Length > 0)
            {
                DataTable info = Bll_ProductIn.GetInStockInfo(cbx_Doc.Text);
                if (info.Rows.Count > 0)
                {
                    var obaStatus = info.Rows[0]["OBAStatus"].ToString();
                    if (obaStatus.Equals("PD"))
                    {
                        MessageBox.Show("请联系质量部门，OBA未确认!");
                        lab_StorageSN.Text = "";
                        cbx_Doc.Text = "";
                        cbx_Doc.Focus();
                        return;
                    }

                    if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                    {
                        lab_StorageSN.Text = info.Rows[0]["Storage_SN"].ToString();

                        Storage_SN = info.Rows[0]["Storage_SN"].ToString();
                        realPartNumber = info.Rows[0]["PartNumber"].ToString();

                        realDateCode = info.Rows[0]["Ext2"].ToString();
                        txt_LocationSN.Focus();

                        lab_msg.Text = GetBillInfoMsg(cbx_Doc.Text);

                    }
                    else
                    {
                        MessageBox.Show(cbx_Doc.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!");
                        lab_StorageSN.Text = "";
                        cbx_Doc.Text = "";
                        cbx_Doc.Focus();
                    }
                }
            }
            else
            {
                lab_msg.Text = cbx_Doc.Text + "单号不正确,请输入正确的单据!";
                cbx_Doc.Focus();
                cbx_Doc.Text = "";

            }
        }


        private void txtLocation_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }
            if (string.IsNullOrEmpty(cbx_Doc.Text))
            {
                lab_msg.Text = "请先选择单号!";
                cbx_Doc.Focus();
                return;
            }

            if (txt_LocationSN.Text.Contains("#"))
            {
                realLocationSN = txt_LocationSN.Text;
            }

            try
            {
                var snArray = realLocationSN.Split('#');

                var storageSN = snArray[0];
                Area_SN = snArray[0] + '#' + snArray[1];

                if (!string.IsNullOrEmpty(Storage_SN) && !Storage_SN.Equals(storageSN))
                {
                    MessageBox.Show("入库仓库" + Storage_SN + "与扫描的仓库" + storageSN + "不一致！");
                    return;
                }

                txt_LocationSN.Text = snArray[1] + snArray[2];

            }
            catch
            {
                MessageBox.Show("库位" + realLocationSN + "格式不正确，请重新输入!");
                txt_LocationSN.Text = "";
                return;
            }

            var InveDT = Bll_ProductIn.GetInventoryInfo(realLocationSN);

            if (InveDT.Rows.Count > 0)
            {
                if (!realPartNumber.Equals(InveDT.Rows[0]["PartNumber"].ToString()))
                {
                    MessageBox.Show("入库库位的物料号" + InveDT.Rows[0]["PartNumber"].ToString() + "与库存的物料号" + realPartNumber + "不一致!");
                    txt_LocationSN.Text = "";
                    return;
                }

                if (!realDateCode.Equals(InveDT.Rows[0]["DateCode"].ToString()))
                {
                    MessageBox.Show("入库库位的DateCode号" + InveDT.Rows[0]["DateCode"].ToString() + "与库存的DateCode号" + realDateCode + "不一致!");
                    txt_LocationSN.Text = "";
                    return;
                }
            }
            else
            {
                var locDt = Bll_ProductIn.QueryLocation(realLocationSN, cbx_Doc.Text);
                if (locDt.Rows.Count == 0)
                {
                    MessageBox.Show(realLocationSN + "库位不正确，请重新输入!");
                    txt_LocationSN.Text = "";
                    return;
                }
            }

            txt_BoxCode.Focus();
        }

        private void txt_BoxCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            if (string.IsNullOrEmpty(cbx_Doc.Text))
            {
                lab_msg.Text = "请先选择单号!";
                cbx_Doc.Focus();
                return;
            }

            if (string.IsNullOrEmpty(realLocationSN))
            {
                MessageBox.Show("请先输入库位并回车!");
                return;
            }



            txt_BoxCode.Text = txt_BoxCode.Text.Split('-').FirstOrDefault();

            DataTable dtIn = Bll_ProductIn.QueryIsIn(cbx_Doc.Text.ToString(), txt_BoxCode.Text);
            if (dtIn.Rows.Count > 0)
            {

                MessageBox.Show(txt_BoxCode.Text + "已入库!");
                txt_BoxCode.Text = "";
                return;
            }

            txt_Qty.Focus();


        }

        private void cbx_Doc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                InputBillNo();
            }
        }

        public string GetBillInfoMsg(string billNo)
        {
            var dt = Bll_ProductIn.GetStorageConfirmInfo(billNo);

            var boxCount = Bll_ProductIn.GetInStockBoxsCount(billNo);
            var result = string.Empty;
            var billType = string.Empty;
            if (dt.Rows.Count > 0)
            {
                billType = dt.Rows[0]["BillType"].ToString();
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("已扫描箱数:" + boxCount);
                var isFinish = false;
                foreach (DataRow dr in dt.Rows)
                {
                    var planQty = 0;
                    if (!string.IsNullOrEmpty(dr["PlanQty"].ToString()))
                    {
                        planQty = Convert.ToInt32(dr["PlanQty"]);
                    }
                    var realQty = 0;
                    if (!string.IsNullOrEmpty(dr["RealQty"].ToString()))
                    {
                        realQty = Convert.ToInt32(dr["RealQty"]);
                    }
                    sb.AppendLine("料号:" + dr["PartNumber"] + "\r\n收货数/计划数:" + realQty + "/" + planQty);

                    if (planQty <= realQty)
                    {
                        isFinish = true;
                    }
                }
                result = sb.ToString();
                if (isFinish)
                {
                    MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

                    DialogResult dr = MessageBox.Show("入库单:" + billNo + "收货数量已满，是否确认完成?", "入库确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

                    if (dr == DialogResult.OK)
                    {
                        Bll_ProductIn.InStockFinish(billNo, billType);
                        BindCBX();
                        lab_StorageSN.Text = "";
                        cbx_Doc.Focus();
                        cbx_Doc.Text = "";
                        txt_BoxCode.Text = "";
                        txt_Qty.Text = "";
                        txt_LocationSN.Text = "";
                    }
                }
            }
            return result;
        }

        private void txt_Qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            if (string.IsNullOrEmpty(cbx_Doc.Text))
            {
                lab_msg.Text = "请先选择单号!";
                cbx_Doc.Focus();
                return;
            }

            if (string.IsNullOrEmpty(realLocationSN))
            {
                MessageBox.Show("请输入库位并回车!");
                return;
            }

            if (string.IsNullOrEmpty(txt_BoxCode.Text))
            {
                MessageBox.Show("请输入箱号!");
                return;
            }

            if (string.IsNullOrEmpty(txt_Qty.Text))
            {
                MessageBox.Show("请输入数量!");
                return;
            }

            txt_Qty.Text = txt_Qty.Text.Split('-').LastOrDefault();

            var qty = 0;

            try
            {
                qty = Convert.ToInt32(Convert.ToDecimal(txt_Qty.Text));
            }
            catch (Exception ex)
            {
                MessageBox.Show("只能输入数字!");
                txt_Qty.Text = "";
                return;
            }

            var isCanAddQty = Bll_ProductIn.InStockIsCanAddQty(cbx_Doc.Text, realPartNumber, qty);

            if (!isCanAddQty)
            {
                MessageBox.Show("物料号" + realPartNumber + "的入库数量" + qty + "不能大于计划数量!");
                txt_Qty.Text = "";
                return;
            }


            bool inStock = Bll_ProductIn.ProductInStock(cbx_Doc.Text, realLocationSN.Split('#').FirstOrDefault(), Area_SN, realLocationSN, txt_BoxCode.Text, qty);

            if (inStock)
            {
                lab_msg.Text = txt_BoxCode.Text + "入库成功！\r\n" + GetBillInfoMsg(cbx_Doc.Text);
                txt_LocationSN.Focus();
                txt_BoxCode.Text = "";
                txt_Qty.Text = "";
            }
            else
            {
                lab_msg.Text = txt_BoxCode.Text + "入库失败!";
                txt_LocationSN.Focus();
                txt_BoxCode.Text = "";
                txt_Qty.Text = "";
            }
        }
    }
}